package org.wu.framework.lazy.orm.database.jpa.repository.support.method;

import org.wu.framework.core.CamelAndUnderLineConverter;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.core.utils.ObjectUtils;
import org.wu.framework.lazy.orm.database.jpa.repository.enums.MethodName2SQLType;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class LazySelectMethodSupport extends AbstractLazyDynamicMethodSimpleQuerySupport {
    /**
     * 支持的方法转换类型
     *
     * @return MethodName2SQLType 集合
     */
    @Override
    protected List<MethodName2SQLType> supportMethodName2SQLTypeList() {
        return List.of(
                MethodName2SQLType.LIST,
                MethodName2SQLType.QUERY,
                MethodName2SQLType.SELECT,
                MethodName2SQLType.PAGE,
                MethodName2SQLType.FIND,
                MethodName2SQLType.GET
        );
    }

    /**
     * 方法名称转换成sql
     *
     * @param method 方法
     * @return sql
     */
    @Override
    public String methodName2SQL(Method method) {
        // selectNameAndAgeById
        String mainTableName = getMainTableName(method);
        String methodName = method.getName();

        MethodName2SQLType methodName2SQLType = getMethodName2SQLType(methodName);

        // selectNameAndAgeById --> select_name_and_age_by_id
        String methodNameToLine = CamelAndUnderLineConverter.humpToLine2(methodName);

        // select_name_and_age_by_id  name_and_age_by_id
        methodNameToLine = methodNameToLine.replaceAll(methodName2SQLType.getStartMethodNamePrefix(), NormalUsedString.EMPTY);

        // byString[0]=name_and_age  byString[1]=id
        String[] byString = methodNameToLine.split(methodKeyBy);


        if (byString.length > 0) {
            String selectSQL = String.format("select * from %s", mainTableName);
            // 关键字段查询
            if (byString.length > 1 && !ObjectUtils.isEmpty(byString[0])) {
                // 查询字段
                String queryColumn = byString[0].replaceFirst(NormalUsedString.UNDERSCORE,NormalUsedString.EMPTY);

                String queryColumnSQL = Arrays.stream(queryColumn.split(methodKeyAnd))
                        .filter(selectColumn -> !ObjectUtils.isEmpty(selectColumn))
                        .collect(Collectors.joining(NormalUsedString.COMMA));
                selectSQL = "select " + queryColumnSQL + " from " + mainTableName;
            }


            String condition = byString[1];
            if (!ObjectUtils.isEmpty(condition)) {
                selectSQL += NormalUsedString.SPACE + NormalUsedString.WHERE + NormalUsedString.SPACE;
                // id_and_name
                // id_and_name_in
                String[] columnList = condition.split(methodKeyAnd);

                String andList = Arrays.stream(columnList)
                        .filter(whereColumn -> !ObjectUtils.isEmpty(whereColumn))
                        .map(whereColumn ->
                                whereColumn +
                                        NormalUsedString.SPACE + (whereColumn.endsWith(methodKeyEndWithIn) ? NormalUsedString.IN : NormalUsedString.EQUALS) +
                                        NormalUsedString.SPACE + NormalUsedString.SPACE +
                                        NormalUsedString.LEFT_BRACE + whereColumn + NormalUsedString.RIGHT_BRACE
                        )
                        .collect(Collectors.joining(NormalUsedString.SPACE + NormalUsedString.AND + NormalUsedString.SPACE));
                return selectSQL + andList;
            } else {
                return selectSQL;
            }


        } else {
            String format = "select * from %s";
            return String.format(format, mainTableName);
        }
    }
}
